package com.zhangdi.demo.rabbitmq.consumer;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;
import com.rabbitmq.client.Envelope;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * @author zhangdi
 * @version 1.0
 * @date 2021.11.2 14:05
 */
public class Receiver {
    public static void main(String[] args) {
        Connection connection = null;
        Channel channel = null;

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("1.12.225.29");
        factory.setVirtualHost("/");
        factory.setPort(5672);
        factory.setUsername("worker0");
        factory.setPassword("worker0");

        try {
            connection = factory.newConnection();
            channel = connection.createChannel();
            channel.queueDeclare("producer", false, false,false, null);
            DeliverCallback callback = (s, delivery) -> {
                String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
                System.out.println("message: "+message +"  tag:"+s);
            };

            channel.basicConsume("producer", false, callback, consumerTag->{});
        } catch (IOException | TimeoutException e) {
            e.printStackTrace();
        }
    }
}
